package com.antdesignbackend.backend.activiti.util;

import com.antdesignbackend.backend.activiti.process.entity.Process;
import com.antdesignbackend.backend.activiti.process.service.ProcessService;
import com.antdesignbackend.backend.activiti.processlist.entity.ProcessList;
import com.antdesignbackend.backend.activiti.processlist.service.ProcessListService;
import com.antdesignbackend.backend.activiti.reviewer.entity.Reviewer;
import com.antdesignbackend.backend.activiti.reviewer.service.ReviewerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ActivitiUtil {

    private static ProcessService processService;
    private static ProcessListService processListService;
    private static ReviewerService reviewerService;

    @Autowired
    public ActivitiUtil(ProcessService processService,ProcessListService processListService,ReviewerService reviewerService){
        ActivitiUtil.processService = processService;
        ActivitiUtil.processListService = processListService;
        ActivitiUtil.reviewerService = reviewerService;
    }

    /**
     * 根据表单标识获得流程id
     * @param form
     * @return
     */
    public static Integer getProcessIdByForm(Integer form){
        QueryWrapper<Process> wrapper = new QueryWrapper<>();
        wrapper.eq("form",form);
        Process process = processService.getOne(wrapper);
        if (process != null)
            return process.getId();
        return null;
    }

    /**
     * 通过流程id和节点顺序获得下一个节点
     * @param processId
     * @param sort
     * @return
     */
    public static ProcessList getProcessList(Integer processId,Integer sort){
        QueryWrapper<ProcessList> wrapper = new QueryWrapper<>();
        wrapper.eq("processId",processId);
        wrapper.gt("sort",sort);
        wrapper.orderByAsc("sort");
        List<ProcessList> list = processListService.list(wrapper);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 通过节点id获得节点下所有审批人的集合，适用于“会签”或“或签”
     * @param processId
     * @return
     */
    public static List<Reviewer> getReviewer(Integer processId){
        QueryWrapper<Reviewer> wrapper = new QueryWrapper<>();
        wrapper.eq("processListId",processId);
        List<Reviewer> list = reviewerService.list(wrapper);
        if (list.size() > 0){
            return list;
        }
        return null;
    }

    /**
     * 通过节点id和审批人顺序获得下一个审批人，适用于“依次审批”
     * @param processListId
     * @param orderId
     * @return
     */
    public static Reviewer getReviewerNext(Integer processListId,Integer orderId){
        QueryWrapper<Reviewer> wrapper = new QueryWrapper<>();
        wrapper.eq("processListId",processListId);
        wrapper.gt("orderId",orderId);
        wrapper.orderByAsc("orderId");
        List<Reviewer> list = reviewerService.list(wrapper);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }
}
